﻿
using System;
using System.ComponentModel;
using System.Windows.Forms;
using GDFlacTool.Common;

namespace GDFlacTools.Controls.UCCase
{
    // Declare a delegate  
    public delegate void CheckedCaseTypeChangedEventHandler(object sender, CheckedCaseTypeChangedEventArgs e); 
    
    public partial class UcCase : UserControl
    {
        [Browsable(true)]
        public event CheckedCaseTypeChangedEventHandler CheckedCaseTypeChanged;

        private CaseType _selectCaseType = CaseType.FirstLetter;

        public UcCase()
        {
            InitializeComponent();
        }

        protected virtual void OnCheckedCaseTypeChanged(CheckedCaseTypeChangedEventArgs e)
        {
            if (CheckedCaseTypeChanged != null)
                CheckedCaseTypeChanged(this, e);
        }     


        [Description("Sets the case checkbox"), Category("UCCase"),DefaultValue(false),Browsable(true)]
        public bool CaseChecked
        {
            get { return chkCase.Checked; }
            set {chkCase.Checked = value;}
        }

        [Description("Sets the case type"), Category("UCCase"), DefaultValue(3), Browsable(true)]
        public CaseType Case
        {
            get { return _selectCaseType; }
            set
            {
                _selectCaseType = value;

                switch (_selectCaseType)
                {
                    case CaseType.Lower:
                        rbtLowerCase.Checked = true;
                        break;
                    case CaseType.Upper:
                        rbtUpperCase.Checked = true;
                        break;
                    case CaseType.FirstLetter:
                        rbtCapitalizeLetter.Checked = true;
                        break;
                    case CaseType.FirstWord:
                        rbtCapitalizeWord.Checked = true;
                        break;
                    default:
                        rbtCapitalizeLetter.Checked = true;
                        break;
                }
            }
        }

        public CaseType GetCaseType()
        {
            if(chkCase.Checked)
            {
                if(rbtLowerCase.Checked)
                {
                    return CaseType.Lower;
                }

                if(rbtUpperCase.Checked)
                {
                    return CaseType.Upper;
                }

                if(rbtCapitalizeLetter.Checked)
                {
                    return CaseType.FirstLetter;
                }

                if(rbtCapitalizeWord.Checked)
                {
                    return CaseType.FirstWord;
                }
            }
            
            return CaseType.None;
        }

        private void chkCase_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCase.Checked)
            {
                grbCase.Enabled = true;
            }
            else
            {
                grbCase.Enabled = false;
            }

            var args = new CheckedCaseTypeChangedEventArgs {IsActive = chkCase.Checked};

            OnCheckedCaseTypeChanged(args);
        }

        private void rbtLowerCase_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCase.Checked)
            {
                var args = new CheckedCaseTypeChangedEventArgs {IsActive = chkCase.Checked};

                OnCheckedCaseTypeChanged(args);
            }
        }

        private void rbtUpperCase_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCase.Checked)
            {
                var args = new CheckedCaseTypeChangedEventArgs { IsActive = chkCase.Checked };

                OnCheckedCaseTypeChanged(args);
            }
        }

        private void rbtCapitalizeLetter_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCase.Checked)
            {
                var args = new CheckedCaseTypeChangedEventArgs { IsActive = chkCase.Checked };

                OnCheckedCaseTypeChanged(args);
            }
        }

        private void rbtCapitalizeWord_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCase.Checked)
            {
                var args = new CheckedCaseTypeChangedEventArgs { IsActive = chkCase.Checked };

                OnCheckedCaseTypeChanged(args);
            }
        }
    }

    public class CheckedCaseTypeChangedEventArgs : EventArgs
    {
        public bool IsActive { get; set; }
    }

    //public enum CaseType
    //{
    //    LowerCase,
    //    UpperCase,
    //    CapitalizeFirstLetter,
    //    CapitalizeFirstWord
    //}
}
